package com.atguigu.gulimall.search.thread;

import java.util.concurrent.*;

public class ThreadTest {

    /*
    10固定核心线程的线程池，容量也是10个
        public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
     */
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println(Thread.currentThread().getId() + " : main...start...");

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("图片。。。");
            return "hello.img";
        }, executor);

        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("属性。。。");
            return "5G，晓龙888";
        }, executor);

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            System.out.println("介绍。。。");
            return "安卓旗舰";
        }, executor);

        CompletableFuture<Object> future = CompletableFuture.anyOf(futureAttr, futureImg, futureDesc);

        // 阻塞等待
        String s = (String) future.get();
        System.out.println(Thread.currentThread().getId() + " : main...end..." + s);
    }

    public static class Callable01 implements Callable<String> {
        @Override
        public String call() throws Exception {
            System.out.println("实现 Callable 接口 + FutureTask （可以拿到返回结果，可以处理异常）");
            return "实现 Callable 接口 + FutureTask （可以拿到返回结果，可以处理异常）";
        }
    }

    public static class Runnable01 implements Runnable {
        @Override
        public void run() {
            System.out.println("实现 Runnable 接口");
        }
    }

    public static class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("继承 Thread");
        }
    }
}
